package com.focusai.efairy.model.dev;

import java.io.Serializable;

/**
 * 设备列表项
 * Created by luo_guangwen on 2017/8/12.
 */

public class DeviceItem implements Serializable {
    public long efairydevice_id;// 设备编号
    public String efairydevice_uuid; // 设备唯一编号
    public String efairydevice_name; // 设备名称
    public String efairydevice_description; // 设备描述
    public String efairydevice_address;// 地址
    public float efairydevice_location_lng;// 经度
    public float efairydevice_location_lat;// 纬度
    public int efairydevice_state; // 设备状态，具体状态参考《网络传输协议》设备状态，0-离线 1-火警 2-预警 3-故障 4-启动 5-屏蔽 6-正常
    public int efairydevice_alarm_id; // 0-离线 1-火警 2-预警 3-故障 4-启动 5-屏蔽 6-正常
    public int efairydevice_is_online; // 是否在线，0-否 1-是

    public String efairydevice_version = ""; // 设备软件版本号2
    public String efairydevice_version_date = ""; // 设备软件版本号1

    /**
     * 主id
     */
    public static String getMainCode(String uuid) {
        if (uuid != null && uuid.length() > 6) {
            return uuid.substring(0, uuid.length() - 6);
        }
        return "";
    }

    /**
     * 从id
     */
    public static String getsecodeCode(String uuid) {
        if (uuid != null && uuid.length() > 6) {
            return toStringHex(uuid.substring(uuid.length() - 6, uuid.length()));
        }
        return "";
    }

    /**
     * 转化十六进制编码为字符串
     *
     * @param s
     * @return
     */
    public static String toStringHex(String s) {
        int[] baKeyword = new int[s.length() / 2];
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                int date = Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
                stringBuilder.append(date);
                if (i != baKeyword.length - 1) {
                    stringBuilder.append("-");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return stringBuilder.toString();
    }

    /**
     * @param secondUuid 字节数组
     * @return 16进制字符串
     * @throws
     * @Title:bytes2HexString
     * @Description: secondUuid 字节数组转16进制字符串
     */
    public static String bytes2HexString(String secondUuid) {
        byte[] b = string2Bytes(secondUuid);
        StringBuffer result = new StringBuffer();
        String hex;
        for (int i = 0; i < b.length; i++) {
            hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            result.append(hex.toUpperCase());
        }
        return result.toString();
    }

    private static byte[] string2Bytes(String secondUuid) {
        String[] array = secondUuid.split("-");
        byte[] bytes = new byte[3];
        bytes[0] = Byte.valueOf(array[0]);
        bytes[1] = Byte.valueOf(array[1]);
        bytes[2] = Byte.valueOf(array[2]);
        return bytes;
    }
}
